define(["jquery","bbcare",
        "scprit/department/vm/assessmentScreening/albertaAssessmentOfInfants.ui.js",
        "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"],
    function ($,$bb,$ui,$servers) {
        var event = {};
        event.tab = function(data,arr){
            var val;
            var _this = this;
            $(".testSelect>div").click(function(){
                _this.isChecked(arr);
                $(this).addClass("checking").removeClass("checked").siblings().removeClass("checking");
                val = $.trim($(this).find("span.ques-tt").html());
                var dt = {
                    "type":$(this).attr("title"),
                    "data":data[val]
                };
                $ui.amisInit({"id":"#question","data":dt});
            });
        };
        event.submit = function(arr,type,userId){
            var _this = this;
            var obj = {};
            var submissionResults = function(){
                var arrProne = [], arrDorsal = [], arrStand = [], arrSeats = [];
                var flagProne = 0 ,flagDorsa = 0 ,flagStand = 0 ,flagSeats = 0;
                for(var i=0;i<arr.length;i++){
                    var typeQ = arr[i].substring(0,5);
                    var id = arr[i].substring(5);
                    if(typeQ == "prone"){
                        arrProne.push(id);
                        flagProne = "俯卧位";
                    }else if(typeQ == "dorsa"){
                        arrDorsal.push(id);
                        flagDorsa = "仰卧位";
                    }else if(typeQ == "stand"){
                        arrStand.push(id);
                        flagStand = "站立位";
                    }else if(typeQ == "seats"){
                        arrSeats.push(id);
                        flagSeats = "坐位";
                    }
                }
                obj.prone = arrProne;
                obj.dorsa = arrDorsal;
                obj.stand = arrStand;
                obj.seats = arrSeats;
                var resArr = [flagProne,flagDorsa,flagSeats,flagStand];
                var resStandard = ["俯卧位","仰卧位","坐位","站立位"];   //位置对应；
                var notDo = _this.isDo( resArr , resStandard);
                //获取分数：
                var scoresParms = $servers.scoreParams(type,obj,'',userId);
                var scoreFn = function(data){
                    $bb.openPage("albertaAssessmentOfInfantsResult",{
                        "userId":userId,
                        "type":type
                    });
                };
                if($.isEmptyObject(notDo)){
                    $servers.getScore(scoresParms,scoreFn);
                }else{
                    var tipCon = notDo.join("、")+"的测评还没有做，确定提交么？";
                    var callFn = function(params){
                        $servers.getScore(scoresParms,scoreFn);
                    };
                    $bb.toastConfirmShow(callFn,'' ,tipCon);
                }
            };
            var topbars = [
                {
                    "icon":"fa fa-print",
                    "name":"提交结果",
                    "callback":submissionResults
                }
            ];
            $bb.toolbar(topbars);
        };
        event.scoreSubmit = function(data,type,userId){
           var arr=[];
            var _this = this;
            _this.tab(data,arr);
            $("#question").on("click",".box",function(){
                var type ;
                $(".testSelect>div").each(function(){
                    if($(this).hasClass('checking')){
                        type = $.trim($(this).find("span.ques-tt").html());
                    }
                });
                var index = $(this).index();
                if($(this).hasClass("select")){
                    $(this).removeClass("select").find("div.symbol").html('<i class="fa fa-minus-circle" aria-hidden="true"></i>');
                    data[type][index].flag = false;
                    var id = $(this).attr("id");
                    arr.splice(arr.indexOf(id),1);
                }else{
                    $(this).addClass("select").find("div.symbol").html('<i class="fa fa-check-circle" aria-hidden="true"></i>');
                    data[type][index].flag = true;
                    arr.push($(this).attr("id"));
                }
            });
            _this.submit(arr,type,userId);
        };
        event.isChecked = function(arr){
            var _this = this;
            var proneArr = [],dorsaArr = [],standArr = [],seatsArr = [];
            if(arr && !$.isEmptyObject(arr)){
                for(var i=0;i<arr.length;i++){
                    var title = arr[i].substring(0,5);
                    var val = Number(arr[i].substring(5));
                    if(title == "prone"){
                        proneArr.push(val);
                        $(".testSelect").find("div[title='prone']").addClass("checked");
                    }else if(title == "dorsa"){
                        dorsaArr.push(val);
                        $(".testSelect").find("div[title='dorsa']").addClass("checked");
                    }else if(title == "stand"){
                        standArr.push(val);
                        $(".testSelect").find("div[title='stand']").addClass("checked");
                    }else if(title == "seats"){
                        seatsArr.push(val);
                        $(".testSelect").find("div[title='seats']").addClass("checked");
                    }
                }
            }
            var scoreProne = _this.countScore(proneArr);
            var scoreDorsa = _this.countScore(dorsaArr);
            var scoreStand = _this.countScore(standArr);
            var scoreSeats = _this.countScore(seatsArr);
            $(".testSelect").find("div").eq(0).find("span.ques-score").html(scoreProne);
            $(".testSelect").find("div").eq(1).find("span.ques-score").html(scoreDorsa);
            $(".testSelect").find("div").eq(2).find("span.ques-score").html(scoreSeats);
            $(".testSelect").find("div").eq(3).find("span.ques-score").html(scoreStand);
        };
        event.viewReport = function(obj){
            var viewResults = function(){
                $bb.openPage("albertaAssessmentOfInfantsResult",obj);
            };
            var topbars = [
                {
                    "icon":"fa fa-eye",
                    "name":"查看结果",
                    "callback":viewResults
                }
            ];
            $bb.toolbar(topbars);
        };
        event.countScore = function(arr){
            var totalScore = 0;
            if(arr instanceof Array && !$.isEmptyObject(arr)){
                var min = Math.min.apply(null, arr);
                var prevScore=min-1;
                var arrScore=arr.length;
                totalScore = Number(prevScore) + Number(arrScore);
            }
            return totalScore == "0" ? '':"("+totalScore+")";
        };
        event.isDo = function( resultArr , standardArr){
            var notDo = [];
            for(var i = 0; i < resultArr.length ; i++){
                if(resultArr[i] != standardArr[i]){
                    notDo.push(standardArr[i]);
                }
            }
            return notDo;
        }
       return event;
    });